package com.supreme.omsserver.controller;

import com.alibaba.fastjson2.JSONObject;
import com.supreme.commonboot.utils.AjaxResp;
import com.supreme.commontools.SleepUtils;
import com.supreme.omsserver.component.BloomFilterService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.redisson.api.RBloomFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping(value = "test")
public class TestController {

    @Resource
    private RedisTemplate<String, Serializable> redisTemplate1;

    @Resource(name = "redisTemplateList")
    private List<RedisTemplate<String, Serializable>> redisTemplateList;

    @Resource
    private BloomFilterService bloomFilterService;

    @Resource
    private ThreadPoolTaskExecutor taskExecutor;

    @Resource
    private HttpServletRequest httpServletRequest;

    @Value("${TEMP_ENV1}")
    private String tempEnv1;

    @GetMapping(value = "hello")
    public AjaxResp hello(@RequestParam(value = "message", defaultValue = "什么都没有") String message) {
        log.trace("hello trace = {}", message);
        log.debug("hello debug = {}", message);
        log.info("hello info = {}", message);
        log.warn("hello warn = {}", message);
        log.error("hello error = {}", message);
        return AjaxResp.instance(message);
    }

    @PostMapping(value = "hello")
    public AjaxResp postHello(@RequestBody JSONObject postData) {
        return AjaxResp.instance(postData);
    }

    @GetMapping(value = "requestInfo")
    public AjaxResp requestInfo() {
        StringBuffer requestURL = httpServletRequest.getRequestURL();
        String remoteAddr = httpServletRequest.getRemoteAddr();
        String remoteHost = httpServletRequest.getRemoteHost();
        int remotePort = httpServletRequest.getRemotePort();
        String remoteUser = httpServletRequest.getRemoteUser();

        String localAddr = httpServletRequest.getLocalAddr();
        String localName = httpServletRequest.getLocalName();
        int localPort = httpServletRequest.getLocalPort();

        return AjaxResp.instance(new JSONObject() {{
            put("requestURL", requestURL);
            put("remoteAddr", remoteAddr);
            put("remoteHost", remoteHost);
            put("remotePort", remotePort);
            put("remoteUser", remoteUser);
            put("localAddr", localAddr);
            put("localName", localName);
            put("localPort", localPort);
        }});
    }

    @GetMapping(value = "showEnv")
    public AjaxResp showEnv() {
        JSONObject jsonObject = new JSONObject()
                .fluentPut("tempEnv1", this.tempEnv1);
        return AjaxResp.instance(jsonObject);
    }

    @GetMapping(value = "fileBeat/{level}")
    public AjaxResp fileBeat(@PathVariable(value = "level") Integer level) {
        String randomAlphabetic = RandomStringUtils.randomAlphabetic(16);
        switch (level) {
            case -1:
                break;

            case 0:
                log.debug(randomAlphabetic);
                log.info(randomAlphabetic);
                log.warn(randomAlphabetic);
                log.error(randomAlphabetic);
                break;

            case 1:
                log.debug(randomAlphabetic);
                break;

            case 2:
                log.info(randomAlphabetic);
                break;

            case 3:
                log.warn(randomAlphabetic);
                break;

            case 4:
                log.error(randomAlphabetic);
                break;
        }

        return AjaxResp.instance(randomAlphabetic);
    }

    @GetMapping(value = "sleep")
    public AjaxResp sleep(@RequestParam(value = "timeout", defaultValue = "1") Long timeout) {
        SleepUtils.sleep(timeout);
        return AjaxResp.instance();
    }

    @GetMapping(value = "setRedisValue/{key}")
    public AjaxResp setRedisValue(@PathVariable(value = "key") String key) {
        String randomAlphabetic = RandomStringUtils.randomAlphabetic(10);
        redisTemplateList.get(0).opsForValue().set(key, randomAlphabetic, 10, TimeUnit.MINUTES);
        redisTemplateList.get(1).opsForValue().set(key, randomAlphabetic, 10, TimeUnit.MINUTES);
        redisTemplateList.get(2).opsForValue().set(key, randomAlphabetic, 10, TimeUnit.MINUTES);
        redisTemplateList.get(3).opsForValue().set(key, randomAlphabetic, 10, TimeUnit.MINUTES);
        redisTemplateList.get(4).opsForValue().set(key, randomAlphabetic, 10, TimeUnit.MINUTES);
        return AjaxResp.instance();
    }

    @GetMapping(value = "setRedisValue/{index}/{key}/{val}")
    public AjaxResp setRedisValue(@PathVariable(value = "index") int index, @PathVariable(value = "key") String key, @PathVariable(value = "val") String val) {
        redisTemplateList.get(index).opsForValue().set(key, val);
        return AjaxResp.instance();
    }

    @GetMapping(value = "getRedisValue/{index}/{key}")
    public AjaxResp getRedisValue(@PathVariable(value = "index") int index, @PathVariable(value = "key") String key) {
        Serializable serializable = redisTemplateList.get(index).opsForValue().get(key);
        return AjaxResp.instance(serializable);
    }

    @GetMapping(value = "debugBF/{type}/{key}/{value}")
    public AjaxResp putToBF(@PathVariable(value = "type") Integer type, @PathVariable(value = "key") String key, @PathVariable(value = "value") String value) {
        RBloomFilter<Object> bloomFilter = bloomFilterService.getBloomFilter(key, 10000, 0.00001);

        boolean result;
        switch (type) {
            case 1:
                result = bloomFilter.add(value);
                break;

            case 2:
                result = bloomFilter.contains(value);
                break;

            default:
                result = Boolean.FALSE;
                break;
        }

        return AjaxResp.instance(result);
    }
}
